package com.zyit.mapper;

import com.zyit.model.Room;
import com.zyit.model.Schedule;
import com.zyit.model.SysUsers;
import com.zyit.model.vo.RegisteredInformation;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ScheduleMapper {


    @Select("select * from schedule where department_id = #{departmentId} and inquiry_time = date(now()) and sign = #{sign} and remaining_quantity>0")
    @Result(property = "sysUsersList",column = "user_id",javaType = List.class,
                many = @Many(select = "com.zyit.mapper.SysUsersMapper.findById",
                    fetchType = FetchType.EAGER))
    List<Schedule> findByDepartmentAndTime(Schedule schedule);


    @Select(" select * from schedule where user_id = #{userid}  and inquiry_time = date(now()) and sign = #{sign}")
    @Result(property = "room",column ="room_id",javaType = Room.class,
                one = @One(select = "com.zyit.mapper.RoomMapper.findByUserId",fetchType = FetchType.LAZY))
    Schedule findByUserIdAndSign(@Param("userid") Long userid,@Param ("sign") Integer sign);

    /***
     * 更具挂号单生成后  医生剩余就诊发生改变
     * @param id
     * @return
     */
    @Update("update schedule set quantity= quantity+1,remaining_quantity=remaining_quantity-1 where id =#{id}")
    int updateNum(Long id);

    /***
     * 挂号信息列表接口
     * @return
     */
    @Select("""
            <script>
                 select s.id,p.`name`patient_name ,p.sex,p.id_card,d.`name` department_name,u.user_name,r.registry_time,r.created_time,r.`status` from schedule s join department d on s.department_id = d.id join sys_users u on s.user_id = u.id join registry r on s.id = r.schedule_id join patient p on r.patient_id = p.id
                    <where>
                        <if test = " idCard != null and idCard != '' " >
                            p.id_card = #{idCard}
                        </if>
                        <if test = " departmentName != null and departmentName != '' " >
                            d.`name` = #{departmentName}
                        </if>
                         <if test = " userName != null and userName != '' " >
                            u.user_name = #{userName}
                        </if>
                         <if test = " status != null and status &gt; 0 " >
                            r.`status` = #{status}
                        </if>
                    </where>
            </script>
            
            """)
    List<RegisteredInformation> findAll(RegisteredInformation ri);

}
